/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.army;

import cz.fi.muni.diplomka.common.Squad;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.MarchingArmy;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MarchingArmyManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MarchingArmyManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class MarchingArmyPage extends WebPage {

    private MarchingArmyManager marchingArmyManager = new MarchingArmyManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private PlayerManager playerManager = new PlayerManagerImpl();
    private String selectedCity = "";
    private List<String> cities;

    public MarchingArmyPage(PageParameters parameters) {
        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));
            PageParameters p = new PageParameters();
            p.add("id", parameters.getString("id"));
            add(new LinkPanel("linkPanel", p));
            add(new ArmyPanel("armyPanel", p));

            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            Form<?> selectCity = new Form<Void>("selectCity") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("cityName", selectedCity);
                    setResponsePage(MarchingArmyPage.class, pageParameters);
                }
            };

            add(selectCity);
            selectCity.add(city);

            List<MarchingArmy> list = new ArrayList<>();


            for (MarchingArmy marchingArmy : marchingArmyManager.getAllMarchingArmiesOfCity(cityManager.getCityByName(selectedCity).getId())) {
                list.add(marchingArmy);
            }

            ListView listview = new ListView("listview", list) {
                @Override
                protected void populateItem(ListItem item) {
                    final MarchingArmy marchingArmy = (MarchingArmy) item.getModelObject();
                    item.add(new Label("name", marchingArmy.getName()));
                    item.add(new Label("owner", cityManager.getOwnerOfCity(marchingArmy.getOwnerCityId()).getNickName() + ", city: " + cityManager.getCityById(marchingArmy.getOwnerCityId()).getName()));
                    item.add(new Label("destination", "[" + marchingArmy.getCoordinates().getX_coordinate() + ", " + marchingArmy.getCoordinates().getY_coordinate() + "]"));
                    item.add(new Label("returning", marchingArmy.isReturning() ? "yes" : "no"));
                    item.add(new Label("movementType", marchingArmy.getMovement().toString()));
                    item.add(new Label("stolenFood", marchingArmy.getStolenFood() + ""));
                    item.add(new Label("stolenGold", marchingArmy.getStolenGold() + ""));
                    item.add(new Label("stolenStone", marchingArmy.getStolenStone() + ""));
                    item.add(new Label("timeOfArrival", marchingArmy.getTimeOfArrival().toString()));
                    String units = "";
                    for (Squad squad : marchingArmy.getArmy().getSquads()) {
                        units += squad.getUnit().getName() + ": " + (int) squad.getCount() + ", threshold: " + (int) squad.getThreshold() + " % ";
                    }
                    item.add(new Label("units", units));
                }
            };
            add(listview);
        }
    }
}